/*
 * @Description: 谷歌分析
 * @Author: kanglin
 * @Date: 2018-08-10 17:56:47
 * @Copyright: Created by PanxSoft.
 */
import API_LIST from 'src/API_LIST';
import { parseQueryString } from 'src/utils/helper';
const ID = 'UA-90011411-8';

const dataLayer = window.dataLayer || [];

export function gTag() {
	if (!API_LIST.APP_DEBUG) {
		dataLayer.push(arguments); // eslint-disable-line
	}
}

gTag('js', new Date());
gTag('config', ID);

window.addEventListener('error', evt => {
  /**
   * @link https://developers.google.com/analytics/devguides/collection/gtagjs/events?hl=zh-cn
   */
  gTag('event', evt.error.stack, {
    event_category: 'window.error',
    event_label: navigator.userAgent
  })
});

export function takeAppOrCustomer(src_array) {
	if (src_array.indexOf('appid') !== -1) {
		return ['appid'];
	}
	if (src_array.indexOf('customer_id') !== -1) {
		return ['customer_id'];
	}
	return [];
}

// export default gTag;

export function normalizeSearch() {
	const search = parseQueryString();
	return takeAppOrCustomer(Object.keys(search))
		.map(key => `${key}/${search[key]}`)
		.join('/');
}
